/***************************************************************************
 *   Copyright (C) 2008 by Camille Gallet, Rémy Hubscher, Regis Strub,     *
 *                         Ali Abdou                                       *
 *                                                                         *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "Cellule.hpp"

Cellule::Cellule(){
    this->selected = 0;
    this->setValue((GameEngine::box)0);
    
    // Taille minimum
    this->setMinimumSize (100, 100);

    this->setIconSize(QSize(80,80));


    // Police de caractères
    QFont font("Purisa", 24);
    this->setFont(font);

    // Curseur
    setCursor(Qt::PointingHandCursor);
    
    this->setCheckable(true);

}

GameEngine::box Cellule::getValue(){
	return this->value;
}

Cellule::Cellule(GameEngine::box val){
    Cellule();
    this->setValue(val);
}

void Cellule::setId(int id)
{
    this->id = id;
}

int Cellule::getId(){
    return this->id;
}

void Cellule::setValue(GameEngine::box val){
    QString color=QString( "background-color: none");

    this->value = val;

    this->setIcon(QIcon());

    switch(val)
    {
    case GameEngine::SQUARE_BLUE:
	this->setText("X");
	break;
    case GameEngine::SQUARE_RED:
	this->setText("O");
	break;
    case GameEngine::SQUARE_ONLY:
	this->setText("");
	break;
    case GameEngine::EMPTY:
	this->setText("");
	this->setIcon(QIcon(":empty.png"));
        color=QString( "background-color: QRgb(54,59,54)");
	break;
    }
    this->setStyleSheet( color);
}

void Cellule::setSelected(int pB){
    this->selected = pB;
    if(pB == 1){
    	 this->setChecked(true);	
   	}else{
 		this->setChecked(false);
  	}
}

int Cellule::getSelected(){
    return this->selected;
}


void Cellule::mousePressEvent( QMouseEvent * event )
{
    if(event->button() == Qt::LeftButton)
    {
    	if(this->selected != 1){
        	this->selected = 2;                 //validation du mouvement avec l'appui sur le bouton droit
        	emit rightClick();
       	}
    }

    if(event->button() == Qt::RightButton)
    {
        if(this->getValue() != GameEngine::EMPTY){

	        // Selection d'une case
	        if(this->selected == 1){
	             setSelected(0);
	         }else{
	             setSelected(1);
	             emit this->leftClick(this);
	         }
         }
    }
}
